package com.future.mayi.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.KeepGoodsAdapter;
import com.future.mayi.model.KeepCateModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.KeepCateListResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.KefuUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;

public class KeepGoodsActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;

    private KeepGoodsAdapter adapter;
    private List<KeepCateModel> cateList;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("养护");
        getRightImg().setImageResource(R.drawable.bottom_kefu);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_keep_goods);
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new KeepGoodsAdapter(this, recyclerView);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {
        recyclerView.refresh();
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
        getRightImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KefuUtils.startActivity(KeepGoodsActivity.this);
            }
        });
    }

    private void getData() {
        new HttpRequest<KeepCateListResponse>()
                .with(this)
                .setApiCode(ApiCst.yhIndex)
                .setListener(new HttpRequest.OnNetworkListener<KeepCateListResponse>() {
                    @Override
                    public void onSuccess(KeepCateListResponse response) {
                        cateList = response.getCateList();
                        HCUtils.refreshListForPage(recyclerView, adapter, cateList, 0, 10);
                    }

                    @Override
                    public void onFail(String message, KeepCateListResponse response) {
                        HCUtils.refreshFail(recyclerView, 1, KeepGoodsActivity.this, message);
                    }
                })
                .start(new KeepCateListResponse());
    }


    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void OnItem(View view, int position) {

    }

    @Override
    public void OnLongItem(View view, int position) {

    }


}
